package com.lfk.demo.Ability;

import com.lfk.justwetools.Utils.Log;
import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Component;
import ohos.agp.components.Button;
import ohos.agp.components.RadioContainer;

import com.lfk.demo.ResourceTable;
import com.lfk.justwetools.Component.NewPaint.PaintView;
import ohos.agp.window.dialog.CommonDialog;
import ohos.agp.window.dialog.IDialog;

public class NewPaintAbility extends Ability {
    private PaintView paintView;
    private PaintView.mode checkit;

    @Override
    protected void onStart(Intent intent) {
        super.onStart(intent);
        setPattern(ResourceTable.Pattern_base);
        setUIContent(ResourceTable.Layout_ability_new_paint);
        paintView = (PaintView) findComponentById(ResourceTable.Id_paint);
        Button button = (Button) findComponentById(ResourceTable.Id_clear_it);
        button.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component v) {
                paintView.clearReUnList();
                paintView.clean();
            }
        });

        findComponentById(ResourceTable.Id_action_settings).setClickedListener(this::onActionSetting);
    }

    private void onActionSetting(Component c) {
        LayoutScatter scatter = LayoutScatter.getInstance(this);
        Component component = scatter.parse(ResourceTable.Layout_choose_type, null, false);
        RadioContainer radioGroup = (RadioContainer) component.findComponentById(ResourceTable.Id_draw_group);
        radioGroup.setMarkChangedListener(new RadioContainer.CheckedStateChangedListener() {
            @Override
            public void onCheckedChanged(RadioContainer radioContainer, int checkedId) {
                checkedId = radioContainer.getComponentAt(checkedId).getId();
                switch (checkedId) {
                    case ResourceTable.Id_draw_circle:
                        checkit = PaintView.mode.CIRCLE;
                        break;
                    case ResourceTable.Id_draw_line:
                        checkit = PaintView.mode.LINE;
                        break;
                    case ResourceTable.Id_draw_path:
                        checkit = PaintView.mode.PATH;
                        break;
                    case ResourceTable.Id_draw_rect:
                        checkit = PaintView.mode.RECT;
                        break;
                }
            }
        });

        CommonDialog alert = new CommonDialog(this)
                .setTitleText("选择模式")
                .setContentCustomComponent(component)
                .setButton(IDialog.BUTTON1, "确认", new IDialog.ClickedListener() {
                    @Override
                    public void onClick(IDialog dialog, int which) {
                        paintView.setDrawBase(checkit);
                        Log.e("check", "" + checkit);
                        dialog.remove();
                    }
                })
                .setButton(IDialog.BUTTON3, "取消", new IDialog.ClickedListener() {
                    @Override
                    public void onClick(IDialog dialog, int which) {
                        dialog.remove();
                    }
                });
        alert.show();
    }
}
